﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Globalization;
using System.ComponentModel;

namespace Invini.Localization
{
    public class LocalizationSettings
    {
        private CultureInfo _culture;
        public CultureInfo Culture
        {
            get { return this._culture; }
            set
            {
                if (this._culture == value)
                    return;

                this._culture = value;
                onCultureChanged(new LocalizationCultureChangedEventArgs(value));
            }
        }

        public static event LocalizationCultureChangedEventHandler CultureChanged;

        public static readonly DependencyProperty DesignCultureProperty = DependencyProperty.RegisterAttached(
          "DesignCulture",
          typeof(CultureInfo),
          typeof(LocalizationSettings),
          new FrameworkPropertyMetadata(default(CultureInfo), FrameworkPropertyMetadataOptions.AffectsRender, designCultureChanged)
        );

        public static CultureInfo GetDesignCulture(DependencyObject element)
        {
            return (CultureInfo)element.GetValue(DesignCultureProperty);
        }
        public static void SetDesignCulture(DependencyObject element, CultureInfo value)
        {
            element.SetValue(DesignCultureProperty, value);
        }

        private static void onCultureChanged(LocalizationCultureChangedEventArgs e)
        {
            if (CultureChanged != null)
                CultureChanged(null, e);
        }

        private static void designCultureChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
                return;

            onCultureChanged(new LocalizationCultureChangedEventArgs((CultureInfo)e.NewValue));
        }
    }
}
